package boun.swe.bob.service;

import boun.swe.bob.entities.Bob_Event;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ememory
 * Date: 27.Ara.2010
 * Time: 18:01:12
 * To change this template use File | Settings | File Templates.
 */

public class AddEventController extends BaseController {
    @RequestMapping(method = RequestMethod.GET)
        public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
            Map<String, Object> myModel = Maps.newHashMap();
            Bob_Event myEvent = new Bob_Event();
            //myEvent= bob_EventDao.findEventByEventId(Integer.parseInt(httpServletRequest.getParameter("event_id")));

        myModel.put("himself", true);
           myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
        return new ModelAndView("addEvent", "model", myModel);
        }

    @RequestMapping(method = RequestMethod.POST)
        public ModelAndView handlePost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
            Map<String, Object> myModel = Maps.newHashMap();

            String resultText="Can't create the event!";
            try {
                Integer userId = getLoggedInUser(httpServletRequest).getUserId();
                String eventName = httpServletRequest.getParameter("eventName");
                String eventDescription = httpServletRequest.getParameter("eventDescription");
                String eventLocation = httpServletRequest.getParameter("eventLocation");

                String startDateStr = httpServletRequest.getParameter("startDate");
                String endDateStr = httpServletRequest.getParameter("endDate");
                if(!Strings.isNullOrEmpty(startDateStr) && !Strings.isNullOrEmpty(endDateStr)){
                    Timestamp startDate = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(startDateStr).getTime());
                    Timestamp endDate = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(endDateStr).getTime());
                    compositeDao.AddEvent(userId, eventName, eventDescription, eventLocation, startDate, endDate);
                    resultText = "Event created!";
                }
                

            }
            catch (Exception exp) {
            }


            myModel.put("resultText",resultText);
            myModel.put("himself", true);
            myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));


        return new ModelAndView("addEvent", "model", myModel);

        }

}